package com.zbcz.crm.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.zbcz.crm.bean.InvitedBean
import com.zbcz.crm.databinding.ItemInvitedBeanListBinding
import com.zbcz.crm.util.onSingleClick
import com.zbcz.crm.util.setRoundedCorners

class InvitedBeanListAdapter(private val dataList: List<InvitedBean>) :
    RecyclerView.Adapter<InvitedBeanListAdapter.InvitedBeanListAdapterHolder>() {

    private val expandedStates = mutableMapOf<Int, Boolean>()

    var onItemFileClickListener: ((Int, String) -> Unit)? = null

    var onItemMaterialClickListener: ((Int, String) -> Unit)? = null

    inner class InvitedBeanListAdapterHolder(private val binding: ItemInvitedBeanListBinding) :
        RecyclerView.ViewHolder(binding.root) {

        init {
            binding.tvFold.setOnClickListener {
                toggleItemExpansion(adapterPosition)
            }
            binding.ivFold.setOnClickListener {
                toggleItemExpansion(adapterPosition)
            }

            binding.tvInvitedPayFileValue.onSingleClick {
                onItemFileClickListener?.invoke(
                    adapterPosition,
                    dataList[adapterPosition].inviteFileUrl
                )
            }
            binding.tvInvitedMaterialValue.onSingleClick {
                onItemMaterialClickListener?.invoke(
                    adapterPosition,
                    dataList[adapterPosition].inviteInfoUrl
                )
            }
        }


        fun bind(data: InvitedBean) {
            binding.run {
//                tvFollowNameValue.text = data.followerName
//                tvFollowTimeValue.text = data.followDate
//                tvFollowContentValue.text = data.followContent
                tvInvitedNameValue.text = data.inviteName
                tvInvitedPayRateValue.text = data.inviteProportion
                tvInvitedPriceValue.text = data.inviteMoney.toString()
            }
            binding.contentParent.setRoundedCorners(45f)
        }

        fun setExpanded(expanded: Boolean) {
            val visible = if (expanded) View.VISIBLE else View.GONE
            binding.clInvitedPayRate.visibility = visible
            binding.clInvitedFile.visibility = visible
            binding.clInvitedMaterial.visibility = visible
            binding.bottomDivider.visibility = visible
        }
    }

    private fun toggleItemExpansion(position: Int) {
        expandedStates[position] = !(expandedStates[position] ?: true)
        notifyItemChanged(position)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): InvitedBeanListAdapterHolder {
        val inflater = LayoutInflater.from(parent.context)
        val binding = ItemInvitedBeanListBinding.inflate(inflater, parent, false)
        return InvitedBeanListAdapterHolder(binding)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: InvitedBeanListAdapterHolder, position: Int) {
        holder.bind(dataList[position])
        // 设置展开或折叠的状态
        holder.setExpanded(expandedStates[position] ?: true)
    }
}